#ifndef RINGBUFF_HPP_
#define RINGBUFF_HPP_

/*******************************************************************************
 ** Name: ringbuff.hpp                                                       **
 ** Description:                                                              **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  Redistribution and use in source and binary forms, with or without       **
 **  modification, are permitted provided that the following conditions are   **
 **  met:                                                                     **
 **                                                                           **
 **  * Redistributions of source code must retain the above copyright notice, **
 **    this list of conditions and the following disclaimer.                  **
 **  * Redistributions in binary form must reproduce the above copyright      **
 **    notice, this list of conditions and the following disclaimer in the    **
 **    documentation and/or other materials provided with the distribution.   **
 **                                                                           **
 **  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      **
 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED **
 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           **
 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT        **
 ** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    **
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  **
 ** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    **
 ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    **
 ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      **
 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        **
 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              **
 **                                                                           **
 ******************************************************************************/
// Created on April 4, 2010, 10:03 AM
// Modified $Date: 2011-09-18 21:48:59 +0000 (Sun, 18 Sep 2011) $ by $Author: gian.james $
#define ringbuff_hpp_ID   "$Id: ringbuff.hpp 145 2011-09-18 21:48:59Z gian.james $"

namespace std
{
template<class T>
class ringbuffer
{
public:
	ringbuffer(uint8_t sz) {
		_sz = sz;
		_buff = new T[_sz];
		_head = _tail = _len = 0;
	}
	virtual ~ringbuffer() { delete [] _buff; }

	void	Put(T data) {
		_buff[_tail++] = data;
		if (_tail >= _sz) _tail = 0;
		++_len;
	}
	T *		Get() {
		if (_len == 0)
			return NULL;
		T c = _buff[_head++];
		if (_head >= _sz) _head = 0;
		--_len;

		return c;
	}
	uint8_t	Size() const { return _sz; }	// how many T's allocated
	uint8_t	Len() const { return _len; }	// how many T's Put()
	T * 	Buff() { return _buff; }
	void	Clear() {
		memset((T*)_buff,0,_sz); _head = _tail = 0;
	}
	uint8_t	Head() const { return _head; }
	uint8_t	Tail() const { return _tail; }

protected:
	T *		_buff;
	uint8_t	_sz;
	uint8_t	_head;
	uint8_t	_tail;
	uint8_t	_len;
};



}
#endif /* RINGBUFF_HPP_ */
